<script lang="ts" setup>
import { getPhotoApi } from '@/api/study/course';
import { useFileTransStore } from '@/stores/fileTransStore';
import { storeToRefs } from 'pinia';
import { computed, h, onMounted, ref, watch } from 'vue';
import Note from '../../components/Note.vue';
import ChapterOverview from './ChapterOverview.vue';
import KeyPointsReview from './KeyPointsReview.vue';
import MindMap from './MindMap2.vue';
import SpeechSummary from './SpeechSummary.vue';
// 定义接口以增强类型安全
interface KeyFrame {
  Start: number;
  End: number;
  FileUrl: string;
  Id: number;
  Summary?: string;
}

interface DataItem {
  time: string;
  content: string[];
  image: string;
  fileUrl: string;
  index: number;
}

const props = defineProps({
  courseId: {
    type: String,
    required: true,
  },
  sourceId: {
    type: String,
    required: true,
  },
});

const fileTransStore = useFileTransStore();
const { keywords, pptExtraction, summarization } = storeToRefs(fileTransStore);

// 展开/收起的状态
const isExpanded = ref(false);

// 存储图片 URL
const imageUrls = ref<string[]>([]);

// 从 summarization 获取标题和摘要
const paragraphTitle = computed(() => {
  return summarization.value?.ParagraphTitle || '未提供标题';
});

const paragraphSummary = computed(() => {
  return summarization.value?.ParagraphSummary || '暂无概述内容';
});

// 显示被截断的摘要
const truncatedSummary = computed(() => {
  const summary = paragraphSummary.value;
  return `${summary.slice(0, 140)}...`;
});

// 提取的 pptExtraction 数据转换为列表项
const data = computed<DataItem[]>(() => {
  if (!pptExtraction.value || !pptExtraction.value.KeyFrameList)
    return [];

  return pptExtraction.value.KeyFrameList.map((item: KeyFrame, index: number) => {
    let contentArray: string[] = ['无内容'];

    if (typeof item.Summary === 'string' && item.Summary.trim() !== '')
      contentArray = item.Summary.split('\n');

    return {
      time: `${Math.floor(item.Start / 60000)}:${((item.Start % 60000) / 1000)
        .toFixed(0)
        .padStart(2, '0')}`,
      content: contentArray,
      image: imageUrls.value[index] || '/jichu.png', // 使用默认图片
      fileUrl: item.FileUrl,
      index,
    };
  });
});

// 请求图片 URL
const fetchImageUrls = async () => {
  const newImageUrls = [...imageUrls.value];
  for (const item of data.value) {
    if (item.fileUrl) {
      try {
        const response = await getPhotoApi(item.fileUrl);
        newImageUrls[item.index] = response; // 获取到的 URL
      }
      catch (error) {
        console.error('获取图片 URL 时出错:', error);
      }
    }
  }
  imageUrls.value = newImageUrls; // 触发响应式更新
};

// 在组件挂载时初始化图片请求
onMounted(() => {
  fetchImageUrls();
});

// 展开/收起处理
const handleExpandClick = () => {
  isExpanded.value = !isExpanded.value;
};

// 自定义 Tab 渲染
const renderTab = (tabTitle: string) => {
  return h('span', { class: 'customTab' }, tabTitle);
};

watch(pptExtraction, () => {
  fetchImageUrls();
});
</script>

<template>
  <div class="container">
    <a-tabs default-active-key="1" class="outerTabs">
      <a-tab-pane key="1" :tab="renderTab('导读')" class="outer">
        <h3 class="keywordsTitle">
          关键词
        </h3>
        <div class="tags">
          <a-tag
            v-for="(keyword, index) in keywords"
            :key="index"
            class="customTag"
            style="font-size: 14px"
          >
            {{ keyword }}
          </a-tag>
        </div>

        <h3>{{ paragraphTitle }}</h3>
        <p>
          {{ isExpanded ? paragraphSummary : truncatedSummary }}
          <span
            style="color: #605ce5; cursor: pointer; margin-left: 10px"
            @click="handleExpandClick"
          >
            {{ isExpanded ? '收起' : '展开全部' }}
          </span>
        </p>

        <a-tabs default-active-key="1" class="innerTabs">
          <a-tab-pane key="2" :tab="renderTab('章节速览')" class="inner">
            <ChapterOverview style="margin-top: 25px;" />
          </a-tab-pane>
          <a-tab-pane key="3" :tab="renderTab('发言总结')" class="inner">
            <SpeechSummary style="margin-top: 25px;" />
          </a-tab-pane>
          <a-tab-pane key="4" :tab="renderTab('要点回顾')" class="inner">
            <KeyPointsReview style="margin-top: 25px;" />
          </a-tab-pane>s
          <a-tab-pane key="5" :tab="renderTab('提取PPT')" class="inner">
            <a-list
              :data-source="data"
              item-layout="vertical"
            >
              <template #renderItem="{ item, index }">
                <a-list-item :key="index" style="display: flex; align-items: center; padding: 16px 0">
                  <div style="flex-shrink: 0; margin-right: 20px; text-align: center">
                    <span style="display: block; font-size: 14px; color: #999">{{ item.time }}</span>
                    <span style="display: inline-block; width: 8px; height: 8px; border-radius: 50%; background-color: #888; margin-top: 8px" />
                  </div>
                  <div style="display: flex; flex: 1">
                    <div style="flex-shrink: 0; margin-right: 16px; width: 200px; height: 130px; overflow: hidden">
                      <img :src="item.image" alt="Card" style="width: 200px; height: 130px; object-fit: cover; border-radius: 4px">
                    </div>
                    <div style="flex: 1">
                      <ul style="list-style: none; padding: 0; margin: 0">
                        <li v-for="(contentItem, idx) in item.content" :key="idx" style="font-size: 14px; margin-bottom: 8px; color: #333">
                          {{ contentItem }}
                        </li>
                      </ul>
                    </div>
                  </div>
                </a-list-item>
              </template>
            </a-list>
          </a-tab-pane>
        </a-tabs>
      </a-tab-pane>
      <a-tab-pane key="2" :tab="renderTab('脑图')">
        <MindMap />
      </a-tab-pane>
      <a-tab-pane key="3" :tab="renderTab('笔记')" class="outer">
        <Note :source-id="sourceId" :course-id="courseId" resource-type="音视频资源" />
      </a-tab-pane>
    </a-tabs>
  </div>
</template>

<style scoped lang="scss">
.container {
    padding: 15px;
    background-color: #ffffff;
    border-radius: 8px;
    margin-top: -20px;
    min-width: 600px;
}

.outerTabs {
  .ant-tabs-nav {
    margin-bottom: 10px;
  }
  .ant-tabs-tab {
    font-size: 16px;
    color: #003399;
  }
  .outer {
    overflow: hidden;
    height: calc(100vh - 180px);
  }
  .ant-tabs-tab-active {
    color: #003399;
    font-weight: bold;
    border-bottom: 2px solid #003399;
  }
}

.innerTabs {
  width:100%;
  padding: 10px 10px; /* 增大点击区域的padding */
  text-align: center; /* 确保文本居中 */
  display: flex; /* 使用flex布局 */
  align-items: center; /* 垂直居中 */
  justify-content: center; /* 水平居中 */
  cursor: pointer; /* 鼠标悬停时显示为手型 */
  font-size: 16px; /* 调整字体大小 */
  transition: background 0.3s ease-in-out; /* 添加背景色的过渡效果 */
  margin-top:-5px;
  .ant-tabs-nav {
    margin-bottom: 16px;
  }
  .ant-tabs-tab {
    font-size: 15px;
    color: #003399;
  }
  .ant-tabs-tab-active {
    color: #003399;
    font-weight: bold;
    border-bottom: 2px solid #003399;
  }
  .inner {
    overflow-x: hidden;
    height: calc(100vh - 180px - 300px);
  }
}

.keywordsTitle {
  font-weight: bold;
  color: #3f3f3f;
}

.customTag {
  background-color: #f6f5fd;
  color: #003399;
  border-radius: 4px;
  margin-right: 6px;
  padding: 5px 13px;
  display: inline-block;
}

.noContent {
  padding: 20px;
  text-align: center;
  color: #888;
}

.content {
  padding: 10px 0;
}

.tags {
  margin-bottom: 30px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.timeAxis {
  position: relative;
  width: 70px;
  text-align: center;
  font-size: 14px;
  color: #003399;
  font-weight: bold;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-right: 20px;
}

.timeAxis::before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  width: 1px;
  height: 300%;
  border-left: 1px dashed #d9d9d9; /* 虚线连接 */
  transform: translateX(-50%);
  z-index: 0;
}

.timeAxis::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 1px;
  height: 100%;
  border-left: 1px dashed #d9d9d9; /* 虚线连接 */
  transform: translateX(-50%);
  z-index: 0;
}

.time {
  color: #333;
  z-index: 1;
  background-color: #fff;
  padding: 2px 6px;
  margin-bottom: 4px;
}

.dot {
  width: 10px;
  height: 10px;
  background-color: #888;
  border-radius: 50%;
  z-index: 1;
  margin-top: 6px;
}

.cardContainer {
  margin-left: 90px; /* 使卡片内容与时间轴点对齐 */
}

.cardTitle {
  font-weight: bold;
  color: #3f3f3f;
  margin-top: -50px;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
}

.cardTitle .time,
.cardTitle .dot {
  margin-left: 10px; /* 调整时间和点与标题的间距 */
}

.card {
  background-color: #f6f5fd;
  border-radius: 8px;
  display: flex;
  padding: 10px;
  align-items: flex-start;
  position: relative;
}
.cardImage {
  flex: 0 0 150px;
  margin-right: 20px;
  align-items: center; /* 垂直居中 */
  margin-top: 20px;
  img {
    width: 100%;
    height: auto;
    border-radius: 8px;
  }
}

.cardContent {
  flex: 1;
}

.cardText {
  font-size: 14px;
  color: black;
  list-style-type: none; /* 移除项目符号 */
  padding-left: 0; /* 移除左边的默认内边距 */
}

.card .time {
  position: absolute;
  top: -25px;
  left: -90px;
  color: #333;
}

.card .dot {
  position: absolute;
  top: -15px;
  left: -50px;
}
.customTab {
  padding: 10px 10px; /* 增大点击区域的padding */
  text-align: center; /* 确保文本居中 */
  display: flex; /* 使用flex布局 */
  align-items: center; /* 垂直居中 */
  justify-content: center; /* 水平居中 */
  cursor: pointer; /* 鼠标悬停时显示为手型 */
  font-size: 16px; /* 调整字体大小 */
  transition: background 0.3s ease-in-out; /* 添加背景色的过渡效果 */
  margin-top:-5px;
}

.customTab:hover {
  background-color: rgba(0, 0, 0, 0.05); /* 悬停时显示背景色 */
  border-radius: 4px; /* 圆角边框 */
}
</style>
